﻿/**
 * 	look.ui.pop 1.0 - jQuery plugin
 *	written by Fangjun Yu
 *
 *	Copyright (c) 2011 ZPCX (http://www.umu.so)
 *
 *	Built for CYGC
 *
 *	本插件需要jquery draggable支持，可直接在页面添加，也可以通过参数传递
 *
 */
(function($) {
	/**
	 *	markup example for $.lookPopDiv();
	 *	
	 *	@param {String} container 弹出交互层html的添加地
	 *	@param {Int} left 距离左侧位移
	 *	@param {Int} top 距离顶部位移
	 *	@param {String} contentHtml 内容
	 *	@param {Boolean} ifRemove 是否移除
	 *	@param {String} addClass 添加样式名
	 *	@param {Boolean} closer 是否有关闭按钮
	 *	@param {Boolean} bodyClick 点击body时是否关闭弹出层
	 *	@param {String} remover 如果移除，则移除该标签对象
	 */
	$.lookPopDiv = function(options){
 
		var defaults = {
			container: "body",
			left: 0,
			top: 0,
			contentHtml: "",
			ifRemove: true,
			addClass: "",
			closer: true,
			bodyClick: false,
			remover: ".look_pop_panel"
		};
		var options = jQuery.extend(defaults, options);
		var panelHtml =
			'<div class="look_pop_panel ' + options.addClass + '" style="top:' + options.top + 'px;left:' + options.left + 'px;">'+
			'	<div class="look_pop_panel_in">'+
			(options.closer ? '<a href="javascript:" class="look_pop_panel_closer">×</a>' : '') +
			'		<div class="look_pop_panel_content">' + options.contentHtml + '</div>'+
			'	</div>'+
			'</div>';

		if(options.ifRemove){
			$(options.remover).remove();
		}
		$(options.container).append(panelHtml);
		$(".look_pop_panel_closer").bind("click",function(){
			$(this).parents(".look_pop_panel").remove();
		});
		$(".look_pop_panel,.look_pop_panel label").bind("click", function(event){
			event.stopPropagation();
		});
		if(options.bodyClick){
			$("body").bind("click", function(){
				$(".look_pop_panel").remove();
			});
		}
	};
	
	$.clearPopDiv = function(options){
		var defaults = {
			id: ".look_pop_panel"
		};
		var options = jQuery.extend(defaults, options);
		$(options.id).remove();
	}
	
	/**
	 *	markup example for $.lookPop();
	 *	
	 *	@param {String}		url				交互层地址链接
	 *	@param {Boolean}	async			是否异步请求（默认为同步）
	 *	@param {String}		errorInfo		如果ajax请求出错，提示信息
	 *	@param {Function}	callback		回调函数
	 *	@param {String}		type			页面类型，url页面[null]。(-1：都没有；0：有取消按钮；1：有确认按钮；2：都有；)
	 *	@param {String}		container		弹出交互层html的添加地
	 *	@param {String}		id				给该弹出层一个唯一标识
	 *	@param {Boolean}	clear			是否移除
	 *	@param {Boolean}	hash			是否添加hash值
	 *	@param {Boolean}	title			弹出层标题，如果有type，则该标题为此type值的大写
	 *	@param {String}		html			弹出层内容
	 *	@param {Boolean}	confirmSubset	是否有确认按钮
	 *	@param {Function}	confirmCallback	确认按钮回调函数
	 *	@param {Boolean}	cancelSubset	是否有取消按钮
	 *	@param {Function}	cancelCallback	取消按钮回调函数
	 *	@param {Boolean}	closeSubset		是否有关闭按钮
	 *	@param {Function}	closeCallback	关闭按钮回调函数
	 *	@param {Object}		focusElement	弹出后的该对象获得焦点
	 *	@param {Boolean}	drag			是否可拖动
	 *	@param {String}		dragJsPath		拖动脚本外部路径
	 *	@param {Boolean}	closer			是否有关闭按钮
	 */
	$.lookPop = function(options){
		var defaults = {
			url: null,
			async: false,//默认同步
			errorInfo: "加载页面出错，请进行刷新，然后再试一次。",
			callback: null,
			type: null,
			container: "body",
			id: "popDialogTable",
			clear: true,
			hash: null,
			title: null,
			html: "",
			confirmSubset: false,
			confirmCallback: null,
			cancelSubset: false,
			cancelCallback: null,
			closeSubset: false,
			closeCallback: null,
			focusElement: null,
			drag: true,
			dragJsPath: "../js/jquery-ui.draggable.min.js",
			closer: true
		};
		var options = jQuery.extend(defaults, options);
		var dialogHtmlFn = function(html){
			$.dialogHtml({
				container: options.container,
				id: options.id,
				clear: options.clear,
				hash: options.hash,
				title: options.title,
				closer: options.closer,
				confirmSubset: options.confirmSubset,
				confirmCallback: options.confirmCallback,
				cancelSubset: options.cancelSubset,
				cancelCallback: options.cancelCallback,
				closeSubset: options.closeSubset,
				closeCallback: options.closeCallback,
				focusElement: options.focusElement,
				drag: options.drag,
				dragJsPath: options.dragJsPath,
				html: html
			});
		}
		if (options.type == null) {
			$.ajax({
				url: options.url,
				cache: false,
				dataType: "text",
				async: options.async,
				error: function(XmlHttpRequest, textStatus, errorThrown){
					$.lookPop({
						type: -1,
						title: "Fail to Get Page",
						closeSubset: true,
						html:
							'<div style="width:500px;">' +
							'	<div class="pop_content_word">' +
							'		<span class="pop_content_span pop_warning_icon">' + options.errorInfo + '</span>' +
							'	</div>' +
							'</div>'
						});
				},
				success: function(html){
					dialogHtmlFn(html);
				},
				complete: function(){
					if (typeof(options.callback) == "function") options.callback();
					$.positionDiv();
				}
			});
		}else{
			if(options.type == 0){
				options.cancelSubset = true;
			}else if(options.type == 1){
				options.confirmSubset = true; 
			}else if (options.type == 2){
				options.confirmSubset = true;
				options.cancelSubset = true;
			}
			options.title = options.title == null ? options.type : options.title;
			dialogHtmlFn(options.html);
			$.positionDiv();
		}
	};
	
	//弹出层html的添加
	$.dialogHtml = function(options){
		var defaults = {
			container: "body",
			html: null,
			id: "popDialogTable",
			clear: true,
			title: null,
			closer: true,
			confirmSubset: false,
			confirmCallback: null,
			cancelSubset: false,
			cancelCallback: null,
			closeSubset: false,
			closeCallback: null,
			focusElement: null,
			drag: true,
			dragJsPath: "../js/jquery-ui.draggable.min.js",
			hash: null
		};
		var options = jQuery.extend(defaults, options), html = "", hash = null;
		options.clear ? $.clearPop({id: (options.clear != true ? options.clear : options.id)}) : null;
		html = (options.title != "" ? "<div class='pop_content_title'>" + options.title + "</div><div class='pop_content_container'>" + options.html + "</div>" : options.html);
		options.hash != null ? hash = '#' + options.hash : hash = (options.closer ? 'javascript:$.clearPop({id:\'' + options.id + '\'});' : 'javascript:');
		var popHtml=
			'<a href="' + hash + '" class="pop_bg"></a>' +
			'<table class="pop_dialog_table" id="' + options.id + '">' +
			'	<tbody>' +
			'		<tr><td class="pop_topleft"></td><td class="pop_border"></td><td class="pop_topright"></td></tr>' +
			'		<tr><td class="pop_border"></td><td>' +
			'			<div class="pop_content_box">' +
							(options.closer ? '<div><a href="' + hash + '" class="pop_close">×</a></div>' : '') +
			'				<div class="pop_content">' + html +
			'					<div class="pop_subset_cell">' +
								(options.confirmSubset ? '<a href="javascript:" class="a_as_button pop_confirm_subset">Confirm</a>　' : '') +
								(options.cancelSubset ?  '<a href="javascript:" class="a_as_button a_as_gray pop_cancel_subset">Cancel</a>' : '') +
								(options.closeSubset ?  '<a href="javascript:" class="a_as_button a_as_gray pop_close_subset">Close</a>' : '') +
			'					</div>' +
			'				</div>' +
			'			</div>' +
			'		</td><td class="pop_border"></td></tr>' +
			'		<tr><td class="pop_bottomleft"></td><td class="pop_border"></td><td class="pop_bottomright"></td></tr>' +
			'	</tbody>' +
			'</table>'
		$(options.container).append(popHtml);
		$("#" + options.id).fadeIn().prev(".pop_bg").css("z-index", parseInt($("#" + options.id).css("z-index")));
		options.focusElement != null ? $(options.focusElement).focus() : $("#" + options.id).find(".pop_confirm_subset").focus();
		if(options.drag){
			var dragFn = function(){
				var handleObject = $("#" + options.id).find(".pop_content_title");
				handleObject.css("cursor", "move");
				$("#" + options.id).draggable({
				 	handle: handleObject,
				 	cursor: "move"
				});
			}
			if(typeof($("#" + options.id).draggable) == "function"){
				dragFn();
			}else{
				$.getScript(options.dragJsPath, function(){
					dragFn();
				});
			}
		}
		if(options.cancelSubset){
			$("#" + options.id).find(".pop_cancel_subset").bind("click", function(){
				options.cancelCallback == null ? $.clearPop({id: options.id}) : typeof(options.cancelCallback) == "function" ? options.cancelCallback($(this)) : null;
			});
		}
		if(options.confirmSubset){
			$("#" + options.id).find(".pop_confirm_subset").bind("click", function(){
				options.confirmCallback == null ? $.clearPop({id: options.id}) : typeof(options.confirmCallback) == "function" ? options.confirmCallback($(this)) : null;
			});
		}
		if(options.closeSubset){
			$("#" + options.id).find(".pop_close_subset").bind("click", function(){
				options.closeCallback == null ? $.clearPop({id: options.id}) : typeof(options.cancelCallback) == "function" ? options.cancelCallback($(this)) : null;
			});
		}
		if($.browser.msie && $.browser.version == "6.0"){
			$(window).scroll(function() {
				$.positionDiv();
			});
		}
		$(window).resize(function(){
			$.positionDiv();
		});
	};
	
	//弹出层的位置重新设置
	$.positionDiv = function() {
		$(".pop_dialog_table").each(function(){
			var windowHeight = $(window).height();
			var windowWidth = $(window).width();
			var thisWidth, thisHeight, topOffset, lefOffset, scrollTop = 0;
			thisWidth = parseFloat($(this).width());
			thisHeight = parseFloat($(this).height());
			if($.browser.msie && $.browser.version == "6.0"){
				scrollTop = $(document).scrollTop();
			}
			$(this).css({top: windowHeight/2 - thisHeight/2 + scrollTop, left: windowWidth/2 - thisWidth/2});
		});
		if($(".pop_content_span").length != 0){
			var iconWidth = parseInt($(".pop_content_span").css("padding-left"));
			$(".pop_content_word").css({width: $(".pop_content_span").width() + iconWidth});
		}
	};

	//清除弹出层方法
	$.clearPop = function(options){
		var defaults = {
			id: "popDialogTable"
		};
		var options = jQuery.extend(defaults, options);
		$("#" + options.id).prev(".pop_bg").remove();
		$("#" + options.id).remove();
	};
})(jQuery);